#Copyright (c) 2025, Alibaba Cloud and its affiliates;
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at

#   http://www.apache.org/licenses/LICENSE-2.0

#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.



import argparse
import fce_util.libs.localrun as localrun
import fce_util.libs.pods as pods

#from libs.pods import get_deployment_pod


def EHS_exec_container_cmd(args):
    """
    EHS execute container fce command entry.
    """
    if args.cmd == 'cmdexec':
        exec_container_cmd(args)
        return

    args.this_parser.print_help()

def _envelope_cmd(args):
    """
    Envelope fce command for container.
    """
    podName = pods.get_deployment_pod(args.clusterId)
    containerName = args.clusterId + pods.ACTION_MAPPING[args.action]["Container"]
    cmd = "kubectl exec {} -c {} -- /usr/local/fce/bin/fceutil {} {}".format(podName, containerName, pods.ACTION_MAPPING[args.action]["Cmd"], ' '.join(args.param))

    return cmd

def exec_container_cmd(args):
    """
    handle argument here.
    """
    envelope_cmd = _envelope_cmd(args)
    status, output = localrun.run(envelope_cmd)
    print(output)

def make(parser):
    """
    Exec fce command in container
    """
    parser.add_argument(
        '--cluster_id',
        dest='clusterId',
        nargs='?',
        help='EHS obtain clusterId',
        )

    parser.add_argument(
        '--action',
        dest='action',
        nargs='?',
        help='EHS fce command action',
        )

    parser.add_argument(
        '--param',
        dest='param',
        nargs=argparse.REMAINDER,
        help='EHS fce command param',
        )

    parser.set_defaults(
        func = EHS_exec_container_cmd,
        this_parser = parser
        )


